package Or

import (
	"context"
	"fmt"
	"db2s/global"
	"db2s/go-log/log"
	"db2s/parDef"
	"db2s/ref"
)

func (sm SeqMe) SeqName(ctx context.Context) (result global.Return, err error) {
	var event = "SeqName"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		return parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, seqSysObjectView, getSeqNameSql, QueryColValManage))
	}
}
func (sm SeqMe) SeqExistence(ctx context.Context) (result global.Return, err error) {
	var event = "[SeqExistence]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, seqSysObjectView, getSeqExistenceSql, QueryColValManage)); err != nil {
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}

func (sm SeqMe) SeqDrop(ctx context.Context) (result global.Return, err error) {
	var event = "[SeqDrop]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	result.Sql = dropSeqSql(schema, table)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.TimeOut, err = newExecUpdateSql("[SeqDrop]", getTableName(schema, table), parDef.GetReadExecSqlFuncOptionsValueBase(ctx), []string{result.Sql})
		return
	}
}
func newSeqPrintInfo1(s any) (res any, err error) {
	switch s.(type) {
	case map[string]any:
		newS := mapNullActive(s.(map[string]any))
		if oldCache, ok := newS["cacheSize"]; ok && oldCache != "0" {
			newS["cacheSize"] = fmt.Sprintf("CACHE %v", oldCache)
		} else {
			newS["cacheSize"] = "NOCACHE"
		}
		if oldOrder := newS["orderFlag"]; oldOrder != "N" {
			newS["orderFlag"] = "ORDER"
		} else {
			newS["orderFlag"] = "NOORDER"
		}
		if oldCycle := newS["cycleFlag"]; oldCycle != "N" {
			newS["cycleFlag"] = "CYCLE"
		} else {
			newS["cycleFlag"] = "NOCYCLE"
		}
		res = newS
	}
	return
}
func (sm SeqMe) SeqDefinition(ctx context.Context) (result global.Return, err error) {
	var event = "[SeqDefinition]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var resultMarge any
	var schema, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, seqSysObjectView, getSeqBodySql, QueryColValManage)); err != nil {
			return
		}
		if resultMarge, err = newSeqPrintInfo1(result.Result); err != nil {
			log.ErrorLog().Error(event, getTableName(schema, table), fmt.Sprintf(" newSeqPrintInfo1  Processing fail. Processing Data is %v, error is %v.", result.Result, err))
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, resultMarge, nil))
	}
}

func (sm SeqMe) SeqCreate(ctx context.Context) (result global.Return, err error) {
	var event = "[SeqCreate]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var getJsonValue = parDef.GetExternalGetJsonValueFunc(parDef.GetFuncParameterValue(ctx, parDef.GetExternalGetJsonValueFuncParameterValue()))
	var meta = parDef.GetFuncParameterValue(ctx, parDef.GetMetaParameterValue())
	var schema, _ = parDef.GetExecFuncParameterValueBase(ctx)
	var getValue = func(name string) (res string) {
		return fmt.Sprintf("%v", parDef.GetJsonNameValue(getJsonValue, meta, name))
	}
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.Result = createSeqBodySql(schema, getValue("name"), getValue("minValue"), getValue("maxValue"), getValue("incrementBy"), getValue("startValue"), getValue("cacheSize"), getValue("orderFlag"), getValue("cycleFlag"))
		return
	}

}
func (sm SeqMe) SeqPrint(ctx context.Context) (result global.Return, err error) {
	var event = "[SeqPrint]"
	defer func() {
		if r := ref.RecoverPanic(event, recover()); err == nil && r != nil {
			err = r
		}
	}()
	var getJsonValue = parDef.GetExternalGetJsonValueFunc(parDef.GetFuncParameterValue(ctx, parDef.GetExternalGetJsonValueFuncParameterValue()))
	var meta = parDef.GetFuncParameterValue(ctx, parDef.GetMetaParameterValue())
	var schema, _ = parDef.GetExecFuncParameterValueBase(ctx)
	var getValue = func(name string) (res string) {
		return fmt.Sprintf("%v", parDef.GetJsonNameValue(getJsonValue, meta, name))
	}
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result.Result = createSeqBodySql(schema, getValue("name"), getValue("minValue"), getValue("maxValue"), getValue("incrementBy"), getValue("startValue"), getValue("cacheSize"), getValue("orderFlag"), getValue("cycleFlag"))
		return
	}
}
